<template>
  <div id="main">
    <h2>学生管理</h2>
    <div class="head">
      <el-button
        type="primary"
        style="margin: 20px"
        @click="handleOpen('添加孩子', 1)"
      >添加学生信息</el-button>

      <el-input
        v-model="searchTableInfo"
        placeholder="请输入搜索内容"
        style="width:240px;float: right;margin-right:20px;"
      />
    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="序号" type="index" align="center">
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="studentName"
          label="孩子姓名"
        />

        <el-table-column
          align="center"
          prop="studentId"
          label="学员编号"
        />

        <!-- <el-table-column
          align="center"
          prop="studentSex"
          label="孩子性别"
          :formatter="showSex"
        /> -->

        <!-- <el-table-column align="center" prop="studentNation" label="孩子民族" /> -->

        <el-table-column
          align="center"
          prop="studentBirth"
          label="出生日期"
        />

        <el-table-column
          align="center"
          prop="studentJoinCenter"
          label="进入中心日期"
        />

        <el-table-column
          align="center"
          prop="studentPhone"
          label="联系电话"
        />

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="handleOpen('修改孩子', 2, Scope.row.studentId)"
            >编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(Scope.row.studentId, Scope.row.studentName)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- --------------------------------------------------------------------------------------------------------- -->
    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="is_add_train"
      :before-close="handleClose_t"
    >
      <el-form ref="train" :model="train" label-position="top" :rules="rules">
        <el-form-item
          prop="orgName"
          label="机构名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="train.orgName" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="timeStart"
          label="开始时间"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="train.timeStart"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item
          prop="timeEnd"
          label="结束时间"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="train.timeEnd"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item prop="effect" label="效果" :label-width="formLabelWidth">
          <el-input v-model="train.effect" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose_t">取 消</el-button>
        <el-button
          v-if="t_editType == 1"
          type="primary"
          @click="handleAddTrain('train')"
        >添 加</el-button>
        <el-button
          v-if="t_editType == 2"
          type="primary"
          @click="handleUpdateTrain('train')"
        >修 改</el-button>
      </div>
    </el-dialog>
    <!-- --------------------------------------------------------------------------------------------------------- -->
    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="is_add_mother"
      :before-close="handleClose_m"
    >
      <el-form ref="mother" :model="mother" label-position="top" :rules="rules">
        <el-form-item
          prop="parentAdd"
          label="家庭住址"
          :label-width="formLabelWidth"
        >
          <el-input v-model="mother.parentAdd" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentBirthtoString"
          label="母亲生日"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="mother.parentBirthtoString"
            type="date"
            placeholder="选择生日"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item
          prop="parentCall"
          label="母亲电话"
          :label-width="formLabelWidth"
        >
          <el-input v-model="mother.parentCall" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentEducationalLevel"
          label="母亲文化水平"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="mother.parentEducationalLevel"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          prop="parentEmail"
          label="母亲邮箱"
          :label-width="formLabelWidth"
        >
          <el-input v-model="mother.parentEmail" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentName"
          label="母亲姓名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="mother.parentName" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentOccupation"
          label="母亲职业"
          :label-width="formLabelWidth"
        >
          <el-input v-model="mother.parentOccupation" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose_m">取 消</el-button>
        <el-button
          type="primary"
          @click="handleAddMother('mother')"
        >添 加</el-button>
      </div>
    </el-dialog>
    <!-- --------------------------------------------------------------------------------------------------------- -->
    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="is_add_father"
      :before-close="handleClose_f"
    >
      <el-form ref="father" :model="father" label-position="top" :rules="rules">
        <el-form-item
          prop="parentAdd"
          label="家庭住址"
          :label-width="formLabelWidth"
        >
          <el-input v-model="father.parentAdd" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentBirthtoString"
          label="父亲生日"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="father.parentBirthtoString"
            type="date"
            placeholder="选择生日"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item
          prop="parentCall"
          label="父亲电话"
          :label-width="formLabelWidth"
        >
          <el-input v-model="father.parentCall" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentEducationalLevel"
          label="父亲水平"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="father.parentEducationalLevel"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          prop="parentEmail"
          label="父亲邮箱"
          :label-width="formLabelWidth"
        >
          <el-input v-model="father.parentEmail" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentName"
          label="父亲姓名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="father.parentName" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentOccupation"
          label="父亲职业"
          :label-width="formLabelWidth"
        >
          <el-input v-model="father.parentOccupation" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose_f">取 消</el-button>
        <el-button
          type="primary"
          @click="handleAddFather('father')"
        >添 加</el-button>
      </div>
    </el-dialog>
    <!-- --------------------------------------------------------------------------------------------------------- -->
    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-form ref="child" :model="child" label-position="top" :rules="rules">
        <el-form-item
          prop="studentName"
          label="学生姓名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="child.studentName" autocomplete="off" />
        </el-form-item>

        <el-form-item
          prop="studentSex"
          label="性别"
          :label-width="formLabelWidth"
        >
          <el-select v-model="child.studentSex" placeholder="学生性别">
            <el-option key="0" label="女" value="0" />
            <el-option key="1" label="男" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="学生头像">
          <el-upload
            class="avatar-uploader"
            name="files"
            :action="url"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过5MB
            </div>
          </el-upload>
          <!-- <img v-if="type==2" :src="org.orgImg" width="200px" height="120px"> -->
        </el-form-item>

        <el-form-item
          prop="studentNation"
          label="学生民族"
          :label-width="formLabelWidth"
        >
          <el-input v-model="child.studentNation" autocomplete="off" />
        </el-form-item>

        <el-form-item
          prop="studentLanguageEnv"
          label="语言环境"
          :label-width="formLabelWidth"
        >
          <el-input v-model="child.studentLanguageEnv" autocomplete="off" />
        </el-form-item>

        <el-form-item
          prop="studentBirth"
          label="出生日期"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="child.studentBirth"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>

        <el-form-item
          prop="studentJoinCenter"
          label="进入中心日期"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="child.studentJoinCenter"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>

        <el-form-item
          prop="studentAddress"
          label="家庭住址"
          :label-width="formLabelWidth"
        >
          <el-input v-model="child.studentAddress" autocomplete="off" />
        </el-form-item>

        <el-form-item
          prop="studentPhone"
          label="联系电话"
          :label-width="formLabelWidth"
        >
          <el-input v-model="child.studentPhone" autocomplete="off" />
        </el-form-item>

        <el-form-item label="父亲姓名" :label-width="formLabelWidth">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item prop="studentFatherId">
              <el-select
                v-model="child.studentFatherId"
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="f in father"
                  :key="f.parentId"
                  :label="f.String"
                  :value="f.parentId"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="addF(1, 1)">添加</el-button>
            </el-form-item>
          </el-form>
        </el-form-item>

        <el-form-item
          prop="studentMotherId"
          label="母亲姓名"
          :label-width="formLabelWidth"
        >
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item prop="studentMotherId">
              <el-select
                v-model="child.studentMotherId"
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="f in mother"
                  :key="f.parentId"
                  :label="f.String"
                  :value="f.parentId"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="addM(1, 1)">添加</el-button>
            </el-form-item>
          </el-form>
        </el-form-item>

        <el-form-item
          prop="studentCare"
          label="主要带养人"
          :label-width="formLabelWidth"
        >
          <el-input v-model="child.studentCare" autocomplete="off" />
        </el-form-item>

        <el-form-item
          prop="studentAccompanyTrainer"
          label="陪同训练人"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="child.studentAccompanyTrainer"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item
          prop="studentBrothers"
          label="有无兄弟姐妹"
          :label-width="formLabelWidth"
        >
          <el-select v-model="child.studentBrothers" placeholder="请选择">
            <el-option key="0" label="无" value="0" />
            <el-option key="1" label="有" value="1" />
          </el-select>
        </el-form-item>

        <el-form-item
          v-if="child.studentBrothers == 1"
          prop="brothersSituation"
          label="兄弟姐妹情况"
          :label-width="formLabelWidth"
        >
          <el-input v-model="child.brothersSituation" autocomplete="off" />
        </el-form-item>

        <el-form-item
          prop="studentTreatHospital"
          label="就诊医院名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="child.studentTreatHospital" autocomplete="off" />
        </el-form-item>

        <el-form-item
          prop="studentTreatCurrentDiagnosis"
          label="目前诊断"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="child.studentTreatCurrentDiagnosis"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>

        <el-form-item
          prop="studentTreatPastTrainingHistoryId"
          label="既往训练史"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="child.studentTreatPastTrainingHistoryId"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>

        <el-form-item
          prop="studentTreatTrainingAge"
          label="起训年龄"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="child.studentTreatTrainingAge"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item
          prop="studentTreatOtherDiseases"
          label="其他疾病"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="child.studentTreatOtherDiseases"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>

        <el-form-item
          prop="studentTreatAttention"
          label="训练注意事项"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="child.studentTreatAttention"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>

        <el-form-item label="既往训练机构记录" :label-width="formLabelWidth">
          <el-button
            icon="el-icon-plus"
            circle
            size="small"
            style="margin-left: 20px"
            @click="handleOpenTrain(1,1,1)"
          />
          <el-table :data="train_data" style="width: 100%">
            <el-table-column prop="orgName" label="名称" width="320" />
            <el-table-column prop="timeStart" label="时间" width="320" />
            <el-table-column prop="effect" label="效果" />
            <el-table-column align="center" label="操作">
              <template v-if="editType == 2" slot-scope="Scope">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  @click="
                    handleOpenTrain(
                      '修改记录',
                      2,
                      Scope.row.trainingInstitutionRecordId
                    )
                  "
                />
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  @click="handleDeleteTrain(Scope.row.trainingInstitutionRecordId)"
                />
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>

      </el-form>
      <!-- @click="handleUpdate('child')" -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>
        <el-button
          v-if="editType == 2"
          type="primary"
          @click="handleUpdate('child')"
        >修 改</el-button>
        <el-button
          v-if="editType == 1"
          type="primary"
          @click="handleAddUser('child')"
        >添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addChild,
  deleteChild,
  updateChild,
  getAllChild,
  getChildById,
  getAllParent,
  addTrain,
  getTrainById,
  editTrain,
  delTrain
} from '@/api/child'
import { addFather, addMother } from '@/api/parent'
export default {
  data() {
    return {
      // form: {
      //   orgName: '',
      //   effect: '',
      //   timeStart: []
      //   // dynamicItem: []
      // },
      url: process.env.VUE_APP_BASE_API + 'file/',
      form: [],
      //   formRules: {
      //     name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
      //     phone: [
      //       { required: true, message: '请输入手机号', trigger: 'blur' },
      //       {
      //         pattern: /^1[34578]\d{9}$/,
      //         message: '目前只支持中国大陆的手机号码'
      //       }
      //     ]
      //   },
      train_data: [],
      sex: [
        {
          value: 0,
          label: '女'
        },
        {
          value: 1,
          label: '男'
        }
      ],
      chi_train: [],
      father: [],
      mother: [],
      String: [],
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      // list: [],
      imageUrl: '',
      tableList: [],
      searchTableInfo: '',
      train: {},
      train_id: [],
      trainingInstitutionRecordId: '',
      listLoading: false,
      dialogFormVisible: false,
      is_add_father: false,
      is_add_mother: false,
      is_add_train: false,
      this_stuid: '',
      jurisdiction: [],
      editType: 1, // 1 添加 2 修改
      t_editType: 1,
      child: {},
      formLabelWidth: '200px',
      rules: {
        childIdcard: [
          { required: true, message: '证件号不能为空', trigger: 'blur' }
        ],
        childMedicalInfo: [
          { required: true, message: '医疗信息不能为空', trigger: 'blur' }
        ],
        childName: [
          { required: true, message: '医疗信息不能为空', trigger: 'blur' }
        ],
        childSex: [
          { required: true, message: '孩子怎么会没有性别呢', trigger: 'blur' }
        ],
        childStatus: [
          { required: true, message: '学生状态不能为空', trigger: 'blur' }
        ],
        childTeachingInfo: [
          { required: true, message: '学生教育信息不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    // a
    // 根据计算属性模糊搜索
    list() {
      const searchTableInfo = this.searchTableInfo

      if (searchTableInfo) {
        // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
        // 注意： filter() 不会对空数组进行检测。
        // 注意： filter() 不会改变原始数组。
        return this.tableList.filter(data => {
          // some() 方法用于检测数组中的元素是否满足指定条件;
          // some() 方法会依次执行数组的每个元素：
          // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
          // 如果没有满足条件的元素，则返回false。
          // 注意： some() 不会对空数组进行检测。
          // 注意： some() 不会改变原始数组。
          return Object.keys(data).some(key => {
            // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
            // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  created() {
    this.fetchData()
    this.getparent()
  },
  methods: {
    showSex(row, column) {
      if (row.studentSex === '1') {
        return '男'
      }
      if (row.studentSex === '0') {
        return '女'
      }
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.obj.split(';')[0]
      this.child.studentPic = this.imageUrl
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg'
      const isImage =
        file.type === 'image/png' ||
        file.type === 'image/jpg' ||
        file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 5

      if (!isImage) {
        this.$message.error('上传图片只能是 JPG或PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 5MB!')
      }
      return isImage && isLt2M
      // return true
    },
    test_str() {
      // var allTrain = new Array()
      // for (let i = 0; i < this.train_id.length; i++) {
      //   allTrain.push(this.train_id[i])
      // }
      var allTrain = this.chi_train.toString()

      this.TrainingInstitutionRecords = allTrain.replace(/,/g, ';')
    },
    // --------------------------------------------动态增加input框---------------------------------------------
    addItem() {
      this.form.push({
        orgName: '',
        time: [],
        effect: ''
      })
    },
    deleteItem(item, index) {
      this.form.splice(index, 1)
    },
    // -------------------------------------------------------------------------------------------------------
    getparent: function() {
      getAllParent(1).then(resp => {
        this.father = resp.obj
      })
      getAllParent(0).then(r => {
        this.mother = r.obj
      })
    },
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {

    },
    fetchData() {
      this.listLoading = true
      getAllChild().then(data => {
        this.tableList = data.obj
        this.listLoading = false
      })
      getAllParent(1).then(f => {
        this.father = f.obj
      })
      getAllParent(0).then(m => {
        this.mother = m.obj
      })
    },
    addParent(sex, childId) {
      this.is_add_father = true
    },
    addF(sex, childId) {
      this.is_add_father = true
    },
    handleOpen(title, type, id) {
      this.child = {}
      this.dialogFormVisible = true
      this.title = title
      this.editType = type
      if (type === 2) {
        this.this_stuid = id
        getChildById(id).then(resp => {
          this.child = resp.obj.student
          this.child.studentBrothers = String(this.child.studentBrothers)
          this.child.studentSex = String(this.child.studentSex)
          this.imageUrl = this.child.studentHeadImages
          this.train = resp.obj.TrainingInstitutionRecords
          // this.train_data = resp.obj.TrainingInstitutionRecords

          for (var i = 0; i < this.train.length; i++) {
            this.train_data.push(this.train[i])
          }
        })
      } else {

      }
    },
    addT(type) {
      this.editType = type
      this.is_add_train = true
    },
    addM(sex, childId) {
      this.is_add_mother = true
    },
    handleDelete(id, username) {
      this.$confirm('确认删除该学生？姓名为' + username).then(() =>
        deleteChild(id).then(data => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil(
            (this.tableList.length - 1) / this.pagesize
          )
          const page =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      )
    },
    handleAddTrain(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          for (let i = 0; i < this.train_data.length; i++) {
            this.chi_train.push(this.train_data[i].trainingInstitutionRecordId)
          }
          this.train.studentId = this.this_stuid
          this.train.timeEndtoString = this.train.timeEnd
          this.train.timeStarttoString = this.train.timeStart
          addTrain(this.train).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })

            this.chi_train.push(resp.obj)

            this.fetchData()
          })
          this.train_data.push(this.train)

          this.train = {}
          this.is_add_train = false
          this.this_stuid = ''
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },

    handleAddTrain_1(formName, item, index) {

      // this.train = {}
      // this.train.studentId = this.this_stuid
      // this.train.timeEndtoString = this.form.dynamicItem[index].timeStart[1]
      // this.train.timeStarttoString = this.form.dynamicItem[index].timeStart[0]
      // this.train.effect = this.form.dynamicItem[index].effect
      // this.train.orgName = this.form.dynamicItem[index].orgName
      // this.train.staffId = localStorage.getItem('this_staff_id')
      // addTrain(this.train).then((resp) => {
      //   console.log(resp)
      //   this.train_id.push(resp.obj)
      //   this.$notify({
      //     title: '操作成功',
      //     type: 'success'
      //   })
      //   this.fetchData()
      // })
      // console.log(this.train_id)
    },
    handleUpdate(formName) {
      var allTrain = this.chi_train.toString()

      this.child.studentTreatTrainingInstitutionRecords = allTrain.replace(
        /,/g,
        ';'
      )
      this.child.studentBirthtoString = this.child.studentBirth
      this.child.studentJoinCentertoString = this.child.studentJoinCenter
      this.$refs[formName].validate(valid => {
        if (valid) {
          updateChild(this.child).then(resp => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
            this.chi_train = []
            this.train_id = []
            this.train_data = []
            this.form = []
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleAddUser(formName) {
      var allTrain = this.chi_train.toString()

      this.child.studentTreatTrainingInstitutionRecords = allTrain.replace(
        /,/g,
        ';'
      )
      this.child.studentBirthtoString = this.child.studentBirth
      this.child.studentJoinCentertoString = this.child.studentJoinCenter
      this.$refs[formName].validate(valid => {
        if (valid) {
          // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa需要得到train_id列表的id（）

          addChild(this.child).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })

            this.fetchData()
            this.train_id = []
            this.form = []
            this.train_data = []
          })
          this.child = []
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleAddMother(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addMother(this.mother).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            getAllParent(0)
            this.fetchData()
          })
          this.mother = {}
          this.is_add_mother = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleAddFather(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addFather(this.father).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })

            this.fetchData()
          })
          this.father = {}
          this.is_add_father = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },

    handleOpenTrain(title, type, id) {
      this.train = {}
      this.is_add_train = true
      this.title = title
      this.t_editType = type
      if (type === 2) {
        getTrainById(id).then(resp => {
          this.train = resp.obj
        })
      }
    },
    handleClose_f() {
      this.is_add_father = false
      this.father = {}
    },
    handleClose_m() {
      this.is_add_mother = false
      this.mother = {}
    },
    handleClose_t() {
      this.is_add_train = false
      this.train = {}
    },
    handleClose() {
      this.dialogFormVisible = false
      this.train_id = []
      this.form = []
      this.child = []
      this.train_data = []
      this.imageUrl = ''
    },
    // handleUpdate(formName) {
    //   var allTrain = this.train_id.toString()
    //   console.log(allTrain)
    //   this.child.studentTreatTrainingInstitutionRecords = allTrain.replace(/,/g, ';')
    //   this.child.studentBirthtoString = this.child.studentBirth
    //   this.child.studentJoinCentertoString = this.child.studentJoinCenter
    //   this.$refs[formName].validate((valid) => {
    //     if (valid) {
    //       console.log(this.child)
    //       updateChild(this.child).then((resp) => {
    //         console.log(resp)
    //         this.$notify({
    //           title: '修改成功',
    //           type: 'success'
    //         })
    //         this.fetchData()
    //         this.train_id = []
    //         this.form.dynamicItem = []
    //       })
    //       this.dialogFormVisible = false
    //       return true
    //     } else {
    //       this.$notify({
    //         title: '请正确填写',
    //         type: 'error'
    //       })
    //       return false
    //     }
    //   })
    // },
    handleDeleteTrain(id) {
      // console.log(this.train[0].trainingInstitutionRecordId, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
      delTrain(id).then(resp => {
        var that = this

        this.$notify({
          title: '删除成功！',
          type: 'success'
        })
        this.fetchData()
        this.train_data = []
        getChildById(that.child.studentId).then(resp => {
          this.child = resp.obj.student
          this.child.studentBrothers = String(this.child.studentBrothers)
          this.child.studentSex = String(this.child.studentSex)
          this.imageUrl = this.child.studentHeadImages
          this.train = resp.obj.TrainingInstitutionRecords

          for (var i = 0; i < this.train.length; i++) {
            this.train_data.push(this.train[i])
          }
        })
      })
      // this.is_add_train = false
      // this.train = {}
      return true
    },
    handleUpdateTrain(formName) {
      var that = this

      // console.log(this.train_data)
      this.$refs[formName].validate(valid => {
        if (valid) {
          editTrain(this.train).then(resp => {
            this.$notify({
              title: '修改成功！',
              type: 'success'
            })
            this.fetchData()
            this.train_data = []
            getChildById(that.child.studentId).then(resp => {
              this.child = resp.obj.student
              this.child.studentBrothers = String(this.child.studentBrothers)
              this.child.studentSex = String(this.child.studentSex)
              this.imageUrl = this.child.studentHeadImages
              this.train = resp.obj.TrainingInstitutionRecords

              for (var i = 0; i < this.train.length; i++) {
                this.train_data.push(this.train[i])
              }
            })
          })
          this.is_add_train = false
          this.train = {}
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}

</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
  border-radius: 50%;
}
</style>
